#!/usr/bin/env python

import os
import sys
import argparse

parser = argparse.ArgumentParser(prog="set-initial-hostname")

parser.add_argument("-c", "--config", action="store", dest="config",
						help="Specify the astrobox config file.")

args = parser.parse_args()

basedir = os.path.dirname(os.path.realpath(__file__))

sys.path.insert(0, os.path.join(basedir, "src"))
sys.path.insert(1, os.path.join(basedir, "src", "ext"))

from octoprint.settings import settings

_settings = settings(init=True, basedir=None, configfile=args.config)

defaultHostnameFile = "%s/default-hostname" % os.path.dirname(_settings._configfile)

if not os.path.exists(defaultHostnameFile):
	import random

	from astroprint.manufacturerpkg import manufacturerPkgManager
	from astroprint.network.debian import DebianNetworkManager

	rmdId = random.randint(0, 9999)

	defaultHostname = '%s-%04d' % (manufacturerPkgManager().variant['network_name'], rmdId)

	with open(defaultHostnameFile, 'w') as f:
		f.write(defaultHostname)

	DebianNetworkManager().setHostname(defaultHostname)
	print "hostname set to %s" % defaultHostname
